<?php
require(dirname(__FILE__) . "/" . "../inc/common.inc.php");
if($action){
    if($action == "submit_data"){
        # 查询活动人数
        if(empty($activity)){
            $return=array('msg'=>'请选择活动','code'=>1);
            echo json_encode($return);
            exit;
        }
        if($activity){
            $activity_arr = count($activity);
            if($activity_arr > 2 ){
                $return=array('msg'=>'最多选择两个活动','code'=>1);
                echo json_encode($return);
                exit;
            }
        }
        $activity = implode(",",$activity);
        $act = $db->query( "select * from chen_activity where id in ('{$activity}') " );
        while ($re = $db->fetch_array($act)){
            if($re['people'] >= 20 ){
                $return=array('msg'=>"活动{$re['id']}的人数已满",'code'=>1);
                echo json_encode($return);
                exit;
            }
        }

        if(empty($name)){
            $return=array('msg'=>"请输入姓名",'code'=>1);
            echo json_encode($return);
            exit;
        }
        if(empty($idcard)){
            $return=array('msg'=>"请输入身份证号码",'code'=>1);
            echo json_encode($return);
            exit;
        }
        if(empty(verifyCard($idcard))){
            $return=array('msg'=>"请输入正确的身份证号码",'code'=>1);
            echo json_encode($return);
            exit;
        }

        # 查询身份证是否已报名
        $is_idcard = $db->get_one( "select * from chen_addfrom where idcard = '{$idcard}' or j_idcard = '{$idcard}' " );
        if($is_idcard){
            $return=array('msg'=>"该身份证已报名,请勿重复报名",'code'=>1);
            echo json_encode($return);
            exit;
        }


        if(empty($school)){
            $return=array('msg'=>"请输入学校",'code'=>1);
            echo ($return);
            exit;
        }
        if(empty($class)){
            $return=array('msg'=>"请输入班级",'code'=>1);
            echo json_encode($return);
            exit;
        }
        if(empty($j_name)){
            $return=array('msg'=>"请输入家长姓名",'code'=>1);
            echo json_encode($return);
            exit;
        }
        if(empty($j_idcard)){
            $return=array('msg'=>"请输入家长身份证",'code'=>1);
            echo json_encode($return);
            exit;
        }
        if(empty(verifyCard($j_idcard))){
            $return=array('msg'=>"请输入正确的家长身份证",'code'=>1);
            echo json_encode($return);
            exit;
        }

        # 查询身份证是否已报名
        $is_j_idcard = $db->get_one( "select * from chen_addfrom where idcard = '{$j_idcard}' or j_idcard = '{$j_idcard}' " );
        if($is_j_idcard){
            $return=array('msg'=>"该家长身份证已报名,请勿重复报名",'code'=>1);
            echo json_encode($return);
            exit;
        }


        if(empty($phone)){
            $return=array('msg'=>"请输入电话号码",'code'=>1);
            echo json_encode($return);
            exit;
        }
        if(empty(checkPhone($phone))){
            $return=array('msg'=>"请输入正确的电话号码",'code'=>1);
            echo json_encode($return);
            exit;
        }

        $where = [
            "activity" => $activity,
            "name" => $name,
            "idcard" => $idcard,
            "school" => $school,
            "class" => $class,
            "sex" => get_sex($idcard),
            "age" => get_age($idcard),
            "j_name" => $j_name,
            "j_idcard" => $j_idcard,
            "phone" => $phone,
            "add_time" => date("Y-m-d H:i"),
        ];
        $where = add_str($where);
        $db->query( " INSERT INTO `chen_addfrom` set {$where} " );
        $id = $db->insert_id();
        # 增加人数
        if($id)
            $activity_arr = explode(",",$activity);
            foreach ($activity_arr as $item){
                $db->query( "update chen_activity set people = people + 1 where id = '{$item}' " );
            }

        $return=array('msg'=>"提交成功",'code'=>0);
        echo json_encode($return);
        exit;
    }
}
?>
<!doctype html>
<html>
    <head>
        <meta charset="utf-8">
        <title>自然教育实践活动</title>
        <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
        <script src="js/rem.js"></script>
        <style>
            * {
                 -webkit-box-sizing:unset;
                 box-sizing:unset;
                 -webkit-user-select:unset;
            }

            body{
                margin:0;padding:0;
                background:rgb(123,191,94);
                font-size:16px;
            }
            .header_div{
                width:100%;
            }
            .header_div img{
                width:100%;
            }
            .apply_cont{
                background:#FFFFFF;
                margin-left:2%;
                margin-right:2%;
                margin-top:20px;
                margin-bottom:20px;
                border:1px solid rgb(15,145,101);
                border-radius:5px;
                position:relative;
                padding-left:10px;
                padding-right:10px;
                padding-top:50px;
                padding-bottom:20px;
            }
            .title_div{
                position:absolute;
                left:0;
                right:0;
                top:-15px;
                text-align:center;
            }
            .title_title{
                width:100px;
                background:#FFFFFF;
                border:2px solid rgb(15,145,101);
                margin:0 auto;
                display:inline-block;
                text-align:center;
                font-size:16px;
                font-weight:bold;
                font-family:微软雅黑;
                color:rgb(15,145,101);
                border-top-left-radius:20px;
                border-bottom-left-radius:20px;
                border-top-right-radius:20px;
                border-bottom-right-radius:20px;
                padding:3px 10px 3px 10px;
            }
            .title_img{
                height:16px;
                vertical-align:middle;
            }
            .title_color{
                color:rgb(15,145,101);
                font-size:16px;
                font-family:微软雅黑;
            }

            /*活动多选样式*/
            .input_radio_demo1{
                position: relative;
                width:100%;
                display:inline-block;
                padding:5px 0 5px 0;
            }
            .radio_demo{opacity: 0;}
            .radio_demo+label {
                position: absolute;
                left: 0;
                top: 3px;
                width: 15px;
                height: 15px;
                border-radius: 50%;
                border: 1px solid rgb(15,145,101);
                line-height:16px;
            }
            /*设置选中的input的样式*/
            /* + 是兄弟选择器,获取选中后的label元素*/
            input:checked+label {
                background-color: rgb(15,145,101);
                border: 1px solid rgb(15,145,101);}
            /* 圆点 */
            input:checked+label::after {
                position: absolute;
                content: "";
                width: 11px;
                height: 11px;
                top: 0;
                left: 0;
                border-radius:50%;
                border: 2px solid #ffffff;
            }
            .input_radio_demo1_span{
                float:left;width:300px;margin-left:22px;
                font-size:14px;
            }
            /*输入框样式*/
            .name_title{}
            .input_text{
                width:100%;
                border:none;
                text-align:right;
                outline:none;
            }
            /*协议多选样式*/
            input[type=checkbox]{
                cursor: pointer;
                position: relative;
                width: 14px;
                height: 14px;
                font-size: 14px;
            }
            input[type=checkbox]::after{
                position: absolute;
                top: 0;
                background-color:#ffffff;
                color:rgb(15,145,101);
                width: 14px;
                height: 14px;
                display: inline-block;
                visibility: visible;
                text-align: center;
                content: ' ';
                border:1px solid rgb(15,145,101);
                border-radius:3px;
            }
            input[type=checkbox]:checked::after{
                content: "✓";
                font-size: 14px;
                font-weight: bold;
                line-height:16px;
            }

            /*返回*/
            .result_button{
                color:#FFFFFF;
                font-size:18px;
                font-weight:bold;
                background:rgb(123,191,94);
                border-radius:5px;
                padding:12px 30px;
                display:inline-block;
            }
            /*提交*/
            .submit_button{
                color:#FFFFFF;
                font-size:18px;
                font-weight:bold;
                background:rgb(15,145,101);
                border-radius:5px;
                padding:12px 65px;
                display:inline-block;
            }
        </style>
    </head>

    <body>
        <div class="header_div">
            <img src="image/apply_header.png">
        </div>
        <div class="apply_cont">
            <form class="form_form">
                <div class="title_div">
                    <span class="title_title">报名须知</span>
                </div>

<!--            -->
                <div>
                    <p>
                        <span><img class="title_img" src="image/title_logo1.png"></span>
                        <span class="title_color">选择活动（最多选择2个活动）</span>
                    </p>

                    <?php
                    $huodong_data = $db->query( "select * from chen_activity where 1=1 order by id asc " );
                    ?>
                    <div>

                        <?php
                        $ii = 0;
                        while ($ress = $db->fetch_array($huodong_data)){
                            $ii++
                        ?>
                        <span class="input_radio_demo1">
                            <input class="radio_demo" id="radio_<?= $ii?>" type="checkbox" name="activity[]" value="<?= $ress['id']?>">
                            <label for="radio_<?= $ii?>">
                                <span class="input_radio_demo1_span"><?= $ii?>、<?= $ress['activity_route']?></span>
                            </label>
                        </span>
                        <?php }?>
                    </div>
                </div>

<!--            -->
                <div style="font-size:14px;">
                    <p>
                        <span><img class="title_img" src="image/title_logo2.png"></span>
                        <span class="title_color">学生信息</span>
                    </p>
                    <div style="border-bottom:1px solid #ececec;padding:7px 0 7px 0;">
                        <span style="float:left;vertical-align:middle;width:18%;">姓名：</span>
                        <div style="float:left;vertical-align:middle;width:82%;">
                            <input type="text" class="input_text" name="name" value="" placeholder="请输入">
                        </div>
                        <div style="clear:both;"></div>
                    </div>
                    <div style="border-bottom:1px solid #ececec;padding:7px 0 7px 0;">
                        <span style="float:left;vertical-align:middle;width:25%;">身份证号：</span>
                        <div style="float:left;vertical-align:middle;width:75%;">
                            <input type="text" class="input_text" name="idcard" maxlength="18" value="" placeholder="请输入">
                        </div>
                        <div style="clear:both;"></div>
                    </div>
                    <div style="border-bottom:1px solid #ececec;padding:7px 0 7px 0;">
                        <span style="float:left;vertical-align:middle;width:25%;">学校名称：</span>
                        <div style="float:left;vertical-align:middle;width:75%;">
                            <input type="text" class="input_text" name="school" value="" placeholder="请输入">
                        </div>
                        <div style="clear:both;"></div>
                    </div>
                    <div style="border-bottom:1px solid #ececec;padding:7px 0 7px 0;">
                        <span style="float:left;vertical-align:middle;width:18%;">班级：</span>
                        <div style="float:left;vertical-align:middle;width:82%;">
                            <input type="text" class="input_text" name="class" value="" placeholder="请输入">
                        </div>
                        <div style="clear:both;"></div>
                    </div>
                </div>

<!--            -->
                <div>
                    <p>
                        <span><img class="title_img" src="image/title_logo3.png"></span>
                        <span class="title_color">家长信息</span>
                    </p>
                    <div style="border-bottom:1px solid #ececec;padding:7px 0 7px 0;">
                        <span style="float:left;vertical-align:middle;width:25%;">家长姓名：</span>
                        <div style="float:left;vertical-align:middle;width:75%;">
                            <input type="text" class="input_text" name="j_name" value="" placeholder="请输入">
                        </div>
                        <div style="clear:both;"></div>
                    </div>
                    <div style="border-bottom:1px solid #ececec;padding:7px 0 7px 0;">
                        <span style="float:left;vertical-align:middle;width:25%;">身份证号：</span>
                        <div style="float:left;vertical-align:middle;width:75%;">
                            <input type="text" class="input_text" name="j_idcard" maxlength="18"  value="" placeholder="请输入">
                        </div>
                        <div style="clear:both;"></div>
                    </div>
                    <div style="border-bottom:1px solid #ececec;padding:7px 0 7px 0;">
                        <span style="float:left;vertical-align:middle;width:25%;">联系电话：</span>
                        <div style="float:left;vertical-align:middle;width:75%;">
                            <input type="text" class="input_text" name="phone" maxlength="11" value="" placeholder="请输入">
                        </div>
                        <div style="clear:both;"></div>
                    </div>
                </div>

<!--            -->
                <div style="font-size:12px;margin-top:20px;">
                    <input type="radio" id="yes_xy" style="vertical-align:middle;" name="is_read" value="1">
                    <label for="yes_xy" style="vertical-align:middle;vertical-align:middle;background-color:unset !important;border:none !important;">我已阅读并同意</label>
                    <a onclick="index_11()" style="color:rgb(242,145,72);text-decoration:underline;">儿童安全监护人责任书</a>
                    <span style="vertical-align:middle;vertical-align:middle;">及</span>
                    <a onclick="index_22()" style="color:rgb(242,145,72);text-decoration:underline;">健康协议</a>
                </div>
            </form>
<!--            -->
            <div style="margin-top:60px;text-align:center;">
                <span class="result_button" onclick="location.href='index.php'">返&nbsp;回</span>&nbsp;&nbsp;&nbsp;&nbsp;
                <span class="submit_button" id="submit">提&nbsp;交</span>
            </div>
        </div>
    </body>

    <script src="js/jquery.js"></script>
    <script src="js/jquery.tips-1.0.2.js"></script>
    <script src="../admin/layui/layui.js"></script>
    <script type="text/javascript">
        layui.use('layer', function(){
            var layer = layui.layer;
        });
        let tips = new ShenTips();

        $(function(){
            var is_sub = 0;
            $('#submit').click(function(){
                var activity = $('input[name="activity[]"]').val();
                var name = $('input[name="name"]').val();
                var idcard = $('input[name="idcard"]').val();
                var school = $('input[name="school"]').val();
                var class_a = $('input[name="class"]').val();
                var j_name = $('input[name="j_name"]').val();
                var j_idcard = $('input[name="j_idcard"]').val();
                var phone = $('input[name="phone"]').val();
                var is_read = $('input[name=is_read]:checkbox').val()
console.log(activity)
                if(!activity){
                    tips.tips({msg:"请选择活动",time: 600});
                    is_sub = 0;
                    return false;
                }

                if(name.length <= '0'){
                    tips.tips({msg:"请填写学生姓名",time: 600});
                    is_sub = 0;
                    return false;
                }
                if(idcard.length <= '0'){
                    tips.tips({msg:"请填写学生身份证号",time: 600});
                    is_sub = 0;
                    return false;
                }
                if(school.length <= '0'){
                    tips.tips({msg:"请填写学校名称",time: 600});
                    is_sub = 0;
                    return false;
                }
                if(class_a.length <= '0'){
                    tips.tips({msg:"请填写学生所在班级",time: 600});
                    is_sub = 0;
                    return false;
                }
                if(j_name.length <= '0'){
                    tips.tips({msg:"请填写家长姓名",time: 600});
                    is_sub = 0;
                    return false;
                }
                if(j_idcard.length <= '0'){
                    tips.tips({msg:"请填写家长身份证号",time: 600});
                    is_sub = 0;
                    return false;
                }
                if(phone.length <= '0'){
                    tips.tips({msg:"请填写家长联系电话",time: 600});
                    is_sub = 0;
                    return false;
                }
                if(!$('input[name=is_read]').is(':checked')){
                    tips.tips({msg:"请勾选我已阅读并同意",time: 600});
                    is_sub = 0;
                    return false;
                }
                if(is_sub == '1'){
                    tips.tips({msg:"处理中，请稍等",time: 600});
                    return false;
                }
                is_sub = 1;

                $.ajax({
                    url:"?action=submit_data",
                    type:"post",
                    data:$('.form_form').serialize(),
                    success:function(data){
                        console.log(data)
                        var data_arr = eval("("+data+")");
                        if(data_arr.code == '0'){
                            tips.tips({msg:data_arr.msg,time: 600});
                            setTimeout(function(){
                                window.location.reload();
                            },2500);
                        }else if(data_arr.code == '1'){
                            tips.tips({msg:data_arr.msg,time: 600});
                            is_sub = 0;
                        }
                    }
                });
            });
        })

    </script>
    <script>
        var num=0,
            checks=[];
        [].push.apply(checks,$(".radio_demo"));
        function changeStatus(status,dom){
            if(status){
                dom.checked=false;
                num--;
                tips.tips({msg:"最多只能选择两个",time: 600});
                return false;
            }
        }

        document.forms[0].onclick=function(e){
            var a=e||window.event;
            var target=a.target||a.srcElement;
            if(target.type=='checkbox'){
                if(target.checked){
                    num++;
                }else{
                    num--;
                }
                changeStatus(num>2,target);
            }
        }
    </script>
    <script>
        function index_11() {
            layer.open({
                title:"儿童安全监护人责任书",
                type: 2,
                area: ['90%', '80%'],
                content: 'index_11.php' //这里content是一个URL，如果你不想让iframe出现滚动条，你还可以content: ['http://sentsin.com', 'no']
            });
        }
        function index_22() {
            layer.open({
                title:"健康协议",
                type: 2,
                area: ['90%', '80%'],
                content: 'index_22.php' //这里content是一个URL，如果你不想让iframe出现滚动条，你还可以content: ['http://sentsin.com', 'no']
            });
        }
    </script>
</html>
